package  
{
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.ui.Keyboard;
	import Parede;
	/**
	 * ...
	 * @author Victor Cruz e Bringel
	 */
	public class Turret extends MovieClip 
	{
		var tiroManager: TiroManager = new TiroManager();
		var isRight:Boolean;
		var isLeft:Boolean;
		var isUp:Boolean;
		var isDown:Boolean;
		var tiroAtivo:Boolean;
		public static var turretDelay: int = 10;
		var contador: int = turretDelay;
		public var turretVida: int = 200;
		public var turretHP: int = turretVida;
		
		public function Turret() 
		{
			addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		public function init(Evento: Event):void
		{
			stage.addEventListener(KeyboardEvent.KEY_DOWN, pressButtom);
			stage.addEventListener(KeyboardEvent.KEY_UP, releaseButtom);
			stage.addEventListener(MouseEvent.MOUSE_DOWN, ativarTiro);
			stage.addEventListener(MouseEvent.MOUSE_UP, desativarTiro);
		}
		
		public function pressButtom(Evento: KeyboardEvent):void
		{
	
			switch(Evento.keyCode)
			{
				case Keyboard.RIGHT: 
					isRight = true;
					break;
					
				case Keyboard.LEFT: 
					isLeft = true; 
					break;
					
				case Keyboard.UP:
					isUp = true;
					break;
					
				case Keyboard.DOWN: 
					isDown = true; 
					break;
					
				case Keyboard.W:
					isUp = true;
					break;
				
				case Keyboard.A:
					isLeft = true;
					break;
				
				case Keyboard.S:
					isDown = true;
					break;
				
				case Keyboard.D:
					isRight = true;
					break;
				
			}	
		}
		
		public function releaseButtom(Evento: KeyboardEvent):void
		{

			
			switch(Evento.keyCode)
			{
				case Keyboard.RIGHT: 
					isRight = false;
					break;
					
				case Keyboard.LEFT: 
					isLeft = false; 
					break;
					
				case Keyboard.UP:
					isUp = false;
					break;
					
				case Keyboard.DOWN: 
					isDown = false; 
					break;
					
				case Keyboard.D: 
					isRight = false;
					break;
					
				case Keyboard.A: 
					isLeft = false; 
					break;
					
				case Keyboard.W:
					isUp = false;
					break;
					
				case Keyboard.S: 
					isDown = false; 
					break;
				
				
			}	
		}
		
		public function update(paredeManager:ParedeManager):void
		{
			validarRotacao();
			validarMovimento(paredeManager);
			validarTiro();
			tiroManager.updateTiros();
		}
		
		public function validarMovimento(paredeManager:ParedeManager):void
		{				
			if (isRight && paredeManager.verifyRight(this.x))
			{
				this.x += 5;
			}
			if (isLeft && paredeManager.verifyLeft(this.x))
			{
				this.x -= 5;
			}
			if (isDown && paredeManager.verifyBot(this.y))
			{
				this.y += 5;
			}
			if (isUp && paredeManager.verifyTop(this.y))
			{
				this.y -= 5;
			}
		}
		
		
		
		public function ativarTiro(Evento:MouseEvent):void
		{
			tiroAtivo = true;
			
			
		}
		
		public function desativarTiro(Evento:MouseEvent):void
		{
			tiroAtivo = false;
		}
		
		public function validarTiro():void
		{
			if (contador >= turretDelay)
			{
				if (tiroAtivo)
				{
					tiroManager.AdicionarTiro(this.x, this.y, this.rotation);
					contador = 0;
				}
			}
			else 
			{
				contador += 1;
			}
			//DESAFIO: Caso o tiro esteja ativo, atire novos tiros utilizando a função
			//tiroManager.AdicionarTiro(this.x, this.y, this.rotation);
			//porém os tiros devem ser criados com um intervalo programável, ou seja,
			//uma variável que controla o intervalo entre os tiros.
			
		}
		
		public function validarRotacao():void
		{
			var angleRadian: Number = Math.atan2((Main.instance.mouseY - Main.instance.stage.stageHeight/2), (Main.instance.mouseX - Main.instance.stage.stageWidth/2));
			var angleDegree: Number = angleRadian * 180 / Math.PI;

			this.rotation = angleDegree;
		}
	}
}